<template>
  <z-paging
    auto-show-back-to-top
    ref="paging"
    v-model="dataList"
    :fixed="false"
    height="100%"
    @query="queryList"
  >
    <block v-for="(item, index) in dataList" :key="item.id">
      <indexlist
        :item="item"
        :index="index"
        :type="item.use == true ? 0 : 3"
        :showBtn="false"
      />
    </block>
  </z-paging>
</template>

<script lang="ts" setup>
import { apiUserCouponList, postgoodscoupon } from "@/api";
import { onLoad } from "@dcloudio/uni-app";
import { ref, shallowRef, watch } from "vue";
import indexlist from "./list.vue";
const props = withDefaults(
  defineProps<{
    codeid: number | undefined | string;
  }>(),
  {}
);

const paging = shallowRef<any>(null);
const dataList: any = ref([]);
const cs = ref(0);
const queryList = async (pageNo: number, pageSize: number) => {
  if (cs.value > 0) {
    paging.value.complete(false);
    return;
  }
  try {
    const res = await postgoodscoupon(props.codeid);
    /* paging.value.complete(res); */
    dataList.value = res;
    cs.value = 2;
  } catch (e) {
    console.log("报错=>", e);
    //TODO handle the exception
    paging.value.complete(false);
  }
};

const cklist: any = ref([]);

uni.$on("xuanzhongs", (item0) => {
  let id = item0.id;
  let index = item0.index;

  if (cklist.value.length == 0) {
    cklist.value.push(id);
  } else {
    if (cklist.value.length < 2) {
      console.log(1);
      let state = false;
      cklist.value.forEach((item) => {
        if (item == id) {
          state = true;
        }
      });
      if (state) {
        console.log(2);
        cklist.value = cklist.value.filter((item) => item !== id);
      } else {
        console.log(3);
        cklist.value.push(id);
      }
    } else {
      console.log(4);
      let dingwei = null;
      dataList.value.forEach((item3: any, index: any) => {
        if (item3.id == id) {
          dingwei = index;
        }
      });
      if (dingwei) {
        dataList.value[dingwei].checked = false;
        console.log(dataList.value[dingwei]);
        cklist.value = cklist.value.filter((item2) => item2 !== id);
      }
    }
  }

  console.log(cklist.value);
  return;
});
</script>
